package com.example.system01.service;

import com.example.system01.dao.SuppliersDao;
import com.example.system01.entity.Devices;
import com.example.system01.entity.Suppliers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class SuppliersService implements SuppliersDao {
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Override
    public int add(Suppliers suppliers) {
        String sql = "insert into suppliers values(null,?,?,?,'0')";
        // 2 调用方法实现
        Object[] args = {suppliers.getSuppliersname(), suppliers.getAddr(),suppliers.getPhone()};
        return jdbcTemplate.update(sql, args);
    }

    @Override
    public int delete(Suppliers suppliers) {
        String sql = "delete from suppliers where Id = ?";
        // 2 调用方法实现
        Object[] args = {suppliers.getId()};
        return jdbcTemplate.update(sql, args);
    }

    @Override
    public int update(Suppliers suppliers) {
        String sql = "update suppliers set suppliersname=?,addr=?,phone=? where Id = ?";
        // 2 调用方法实现
        Object[] args = {suppliers.getSuppliersname(),suppliers.getAddr(),suppliers.getPhone(),suppliers.getId()};
        return jdbcTemplate.update(sql, args);
    }

    @Override
    public Suppliers findOne(Suppliers suppliers) {
        String sql = "select * from suppliers where Id="+suppliers.getId().toString();
        return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(Suppliers.class));
    }

    @Override
    public List<Suppliers> findAll() {
        String sql = "select * from suppliers";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Suppliers.class));
    }
}
